﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Phoenix.Service;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.Common.Util;
using Phoenix.Enum;
namespace Phoenix.AdminSite.Controllers
{
    public class AdminUserController : ControllerBase
    {
        IAdminUserService IAdminUser = new AdminUserService();
        IOrganizationService IOrganization = new OrganizationService();
        IIDCreaterService IIDCreater = new IDCreaterService();

        public ActionResult Index()
        {
            int totalRecord = 0;

            ViewData["AdminUserList"] = IAdminUser.GetPagedList(10, 1, "", out totalRecord);

            return View();
          
        }

        public ActionResult Edit()
        {
            int userID = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["UserID"]))
            {
                userID = int.Parse(Request.QueryString["UserID"]);

                ViewData["AdminUser"] = IAdminUser.GetByUserID(userID);
            }

            ViewData["OrganizationList"] = IOrganization.GetPath();

            return View();

        }

        public ActionResult Add()
        {
            ViewData["OrganizationList"] = IOrganization.GetPath();
            return View();
        }

        public ActionResult Update(FormCollection collection)
        {

            if (!string.IsNullOrEmpty(collection["hdUserID"]))
            {
                int userID = int.Parse(collection["hdUserID"]);

                AdminUserModel model = IAdminUser.GetByUserID(userID);
                model.TrueName = collection["txtTrueName"];
                model.EMail = collection["txtEMail"];
                model.OrgID = int.Parse(collection["sltOrg"]);
                model.Memo = collection["txtMemo"];
                model.Telephone = collection["txtTelephone"];
                model.UpdateTime = DateTime.Now;
                IAdminUser.UpdateByUserID(model);
                return ShowMsg("成功", Common.ActionStatus.OK, "");
            }
            else
            {
                return ShowMsg("失败", Common.ActionStatus.Error, "");
            }
           
        }

        public ActionResult Save(FormCollection collection)
        {
            AdminUserModel model = new AdminUserModel();
            model.UserID = IIDCreater.GetNewID("AdminID", "管理员编号", 1000);
            model.UserName = collection["txtUserName"];
            model.TrueName = collection["txtTrueName"];
            model.EMail = collection["txtEMail"];
            model.OrgID = int.Parse(collection["sltOrg"]);
            model.Memo = collection["txtMemo"];
            model.Password = collection["txtPassword"];
            model.Telephone = collection["txtTelephone"];
            model.UpdateTime = DateTime.MinValue;
            model.Visitation = 0;
            model.LastLoginTime = DateTime.MinValue;
            model.CreateTime = DateTime.Now;
            model.CreateUser = 1;
            model.DataPermisType = 1;
            model.DeleteFlag = false;
            model.DeleteTime = DateTime.MinValue;
            model.DeleteUser = 1;
            model.LockTime = DateTime.MinValue;
            model.Status = true; ;
            
            IAdminUser.AddAdminUser(model);

            return ShowMsg("成功", Common.ActionStatus.OK, "");

        }

        //检测用户名是否存在
        public ActionResult CheckUserName()
        {
            string userName = Request.QueryString["txtUserName"];
            bool b = IAdminUser.GetExistsUserName(userName);

            return Content(b == true ? "1" : "0");
        }


    }
}
